package org.jeecg.modules.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.maps.model.param.BuildFloorQuery;
import org.jeecg.modules.model.entity.BimModelUnit;
import org.jeecg.modules.model.model.params.BimModelUnitBatchAddParam;
import org.jeecg.modules.model.model.params.BimModelUnitParam;
import org.jeecg.modules.model.model.params.BimModelUnitTagParam;
import org.jeecg.modules.model.model.result.BimModelUnitBatchAddResult;
import org.jeecg.modules.model.service.IBimModelUnitService;
import org.jeecg.modules.project.entity.BuildResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 支节点-构件编码
 * @Author: jeecg-boot
 * @Date: 2023-04-18
 * @Version: V1.0
 */
@Api(tags = "支节点-构件管理")
@RestController
@RequestMapping("/model/unit")
@Slf4j
public class BimModelUnitController extends JeecgController<BimModelUnit, IBimModelUnitService> {

    @Autowired
    private IBimModelUnitService bimModelUnitService;

    /**
     * 模型树编辑根据工程管理编码查询楼栋楼层模型树
     *
     * @param projectInfoId
     * @return bimProjectBuildResult
     */
    @ApiOperation(value = "模型树编辑根据工程管理编码查询楼栋楼层模型树", notes = "模型树编辑根据工程管理编码查询楼栋楼层模型树信息")
    @GetMapping(value = "/tree")
    public Result<?> query(@RequestParam(name = "projectInfoId") Integer projectInfoId,
                           @RequestParam(name = "buildId", required = false) Integer buildId,
                           @RequestParam(name = "floorId", required = false) Integer floorId) {
        BuildFloorQuery query = new BuildFloorQuery();
        query.setProjectInfoId(projectInfoId);
        query.setBuildId(buildId);
        query.setFloorId(floorId);
        return Result.OK(bimModelUnitService.getProjBuildInfos(query, BuildResultType.UNIT));
    }

    /**
     * 根据条件查询对应信息完成匹配后的数组列表返回
     *
     * @param modelUnitParam
     * @return Result<?>
     */
    @ApiOperation(value = "根据条件查询对应信息完成匹配后的数组列表返回", notes = "根据条件查询对应信息完成匹配后的数组列表返回")
    @GetMapping(value = "/query")
    public Result<?> query(BimModelUnitParam modelUnitParam) {
        return Result.OK(bimModelUnitService.queryUnits(modelUnitParam));
    }

    /**
     * 分页列表查询
     *
     * @param bimModelUnit
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "支节点-构件分页列表查询")
    @ApiOperation(value = "支节点-构件分页列表查询", notes = "支节点-构件分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BimModelUnit bimModelUnit,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BimModelUnit> queryWrapper = QueryGenerator.initQueryWrapper(bimModelUnit, req.getParameterMap());
        Page<BimModelUnit> page = new Page<BimModelUnit>(pageNo, pageSize);
        IPage<BimModelUnit> pageList = bimModelUnitService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 设置构件标签
     *
     * @param unitTagParam
     * @return
     */
    @AutoLog(value = "设置构件标签")
    @ApiOperation(value = "设置构件标签", notes = "设置构件标签")
    @PutMapping(value = "/tag")
    public Result<?> tag(@RequestBody BimModelUnitTagParam unitTagParam) {
        bimModelUnitService.tagNode(unitTagParam);
        return Result.OK("设置构件标签成功！");
    }

    /**
     * 添加
     *
     * @param bimModelUnit
     * @return
     */
    @AutoLog(value = "支节点-构件添加")
    @ApiOperation(value = "支节点-构件添加", notes = "支节点-构件添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BimModelUnit bimModelUnit) {
        bimModelUnitService.add(bimModelUnit);
        return Result.OK("添加成功！");
    }


    /**
     * 批量添加
     *
     * @param batchAddParam
     * @return batchAddResult
     */
    @AutoLog(value = "支节点-构件批量添加")
    @ApiOperation(value = "支节点-构件批量添加", notes = "支节点-构件批量添加")
    @PostMapping(value = "/batchAdd")
    public Result<?> batchAdd(@RequestBody BimModelUnitBatchAddParam batchAddParam) {
        BimModelUnitBatchAddResult batchAddResult = bimModelUnitService.batchAdd(batchAddParam);
        return Result.OK(batchAddResult);
    }

    /**
     * 编辑
     *
     * @param bimModelUnitList
     * @return
     */
    @AutoLog(value = "支节点-构件编辑")
    @ApiOperation(value = "支节点-构件编辑", notes = "支节点-构件编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody List<BimModelUnit> bimModelUnitList) {
        bimModelUnitService.updateBatchById(bimModelUnitList);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "支节点-构件通过id删除")
    @ApiOperation(value = "支节点-构件通过id删除", notes = "支节点-构件通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        bimModelUnitService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "支节点-构件批量删除")
    @ApiOperation(value = "支节点-构件批量删除", notes = "支节点-构件批量删除")
    @ApiImplicitParam(name = "ids", value="批量删除构件编码,多个以逗号分隔",required = true)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.bimModelUnitService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "支节点-构件通过id查询")
    @ApiOperation(value = "支节点-构件通过id查询", notes = "支节点-构件通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BimModelUnit bimModelUnit = bimModelUnitService.getById(id);
        if (bimModelUnit == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(bimModelUnit);
    }

}
